﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons
{
    /// <summary>
    /// 输出一个九宫格容器控件
    /// </summary>
    public class PageCommon_GridLayout: ControlBase
    {
        private List<GridItemConfig> _lst=new List<GridItemConfig>();

        public PageCommon_GridLayout(ViewContext viewContext) : base(viewContext)
        {
            _attrs.Class("weui-grids");

            //this.Style("overflow", "auto");
        }

        public PageCommon_GridLayout AddItem(Action<GridItemConfig> configFunc)
        {
            var config = new GridItemConfig();

            configFunc(config);

            _lst.Add(config);

            return this;
        }

        public override MvcHtmlString Render()
        {
            ViewContext.Writer.WriteLine($"<div {_attrs}>");

            foreach (var config in _lst)
            {
                ViewContext.Writer.WriteLine($"<a {(string.IsNullOrWhiteSpace(config._href) ? "" : $"href=\"{config._href}\"")} {(string.IsNullOrWhiteSpace(config._onclick) ? "" : $"onclick=\"{config._onclick}\"")} class=\"weui-grid js_grid\">");

                ViewContext.Writer.WriteLine($"<span class=\"weui-badge\" style=\"{(config._badge<=0).If("display: none;")} position: absolute;top: 0em;right: 0.0em;z-index: 99;\">{config._badge}</span>");

                ViewContext.Writer.WriteLine("<div class=\"weui-grid__icon\">");
                ViewContext.Writer.WriteLine($"<img src=\"{config._icon}\" alt=\"\">");
                ViewContext.Writer.WriteLine("</div> <!-- End grid__icon -->");

                ViewContext.Writer.WriteLine($"<p class=\"weui-grid__label\">{config._title}</p>");


                ViewContext.Writer.WriteLine("</a> <!--End weui-grid -->");
            }

            ViewContext.Writer.WriteLine("</div> <!-- weui-grids -->");

            return MvcHtmlString.Empty;
        }
        

        public class GridItemConfig
        {
            internal string _title = "";
            internal string _icon = "";
            internal string _href = "";
            internal string _onclick = "";
            internal int _badge = 0;

            public GridItemConfig Title(string title)
            {
                _title = title;

                return this;
            }

            public GridItemConfig Title(Func<GridItemConfig, HelperResult> title)
            {
                _title = title(this).ToHtmlString();

                return this;
            }

            public GridItemConfig Icon(string iconUrl)
            {
                _icon = iconUrl;

                return this;
            }

            /// <summary>
            /// 点击后的跳转链接
            /// </summary>
            /// <param name="href"></param>
            /// <returns></returns>
            public GridItemConfig Href(string href)
            {
                _href = href;
                _onclick = "";

                return this;
            }

            /// <summary>
            /// 点击按钮时的操作,与Href二选一
            /// </summary>
            /// <param name="clickScript"></param>
            /// <returns></returns>
            public GridItemConfig OnClick(string clickScript)
            {
                _onclick = clickScript;
                _href = "";

                return this;
            }

            public GridItemConfig Badge(int badgeValue)
            {
                _badge = badgeValue;

                return this;
            }

        }

    }
}